$(function () {
    $("#orgPositionGrid").Grid({
        url: '../org/position/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '所属公司', name: 'companyName', index: 'company_id', width: 80},
			{label: '所属部门', name: 'departmentName', index: 'department_id', width: 80},
			{label: '名称', name: 'name', index: 'name', width: 80},
			{label: '排序', name: 'orderNum', index: 'order_num', width: 80}]
    });
//    vm.getPosition();
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    }
};
var ztree;

var vm = new Vue({
	el: '#orgPosition',
	data: {
        showList: true,
        title: null,
		orgPosition: {},
		positionList: [],
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			],
			companyId:[
	           {required: true, message: '公司不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: '',companyName:'',departmentName:''
		},
		departmentList: []
	},
	methods: {
		/*queryPositionList:function(val){
			if(val){
				var addResult=true;
				for(var i=0;i<vm.positionList.length;i++){
					if(vm.positionList[i].dictValue==val){
						addResult=false;
					}
				}
				if(addResult){
					vm.positionList.push({'dictKey':val,'dictValue':val});
				}
			}
		},*/
		/*getPosition: function () {
            Ajax.request({
                url: '../sys/dict/queryAll',
                params: {
                    'groupCode': 'ORG_DUTY'
                },
                async: true,
                successCallback: function (r) {
                    vm.positionList = r.list;
                }
            });
        },*/
        getCompany: function () {
            //加载部门树
            Ajax.request({
                url: "../org/company/select",
                async: true,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#orgCompanyTree"), setting, r.deptList);
                    var node = ztree.getNodeByParam("deptId", vm.orgPosition.companyId);
                    if (node) {
                        ztree.selectNode(node);
                        vm.orgPosition.companyName = node.name;
                    } else {
                        node = ztree.getNodeByParam("deptId", 'root');
                        ztree.selectNode(node);
                        vm.orgPosition.companyName = node.name;
                    }
                }
            });
        },
        getDepartment: function () {
        	//加载部门树
        	Ajax.request({
        		url: "../org/department/queryAll?companyId="+vm.orgPosition.companyId,
        		async: true,
        		successCallback: function (r) {
        			vm.departmentList=r.list;
        		}
        	});
        },
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.orgPosition = {};
			vm.getCompany();
		},
		update: function (event) {
            var id = getSelectedRow("#orgPositionGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
		},
		saveOrUpdate: function (event) {
            var url = vm.orgPosition.id == null ? "../org/position/save" : "../org/position/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.orgPosition),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
        deptTree: function () {
            openWindow({
                title: "选择上级",
                area: ['300px', '450px'],
                content: jQuery("#orgCompanyLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    vm.orgPosition.companyId = node[0].deptId;
                    vm.orgPosition.companyName = node[0].name;
                    //设置公司下所有的部门
                    vm.getDepartment();
                    layer.close(index);
                }
            });
        },
		del: function (event) {
            var ids = getSelectedRows("#orgPositionGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../org/position/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../org/position/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.orgPosition = r.orgPosition;
                    vm.getDepartment();
                    vm.getCompany();
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            var page = $("#orgPositionGrid").jqGrid('getGridParam', 'page');
			$("#orgPositionGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name,'companyName': vm.q.companyName,'departmentName': vm.q.departmentName},
                page: 1
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {name: '',companyName:'',departmentName:''};
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});